package com.jihox.abookpro;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.jihox.abookpro.R;
import com.jihox.abookpro.fragment.AssertFragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

public class SubmitSuccessActivity extends BaseActivity {
	
	private RadioButton shareButton;
	private AssertFragment assertFragment;
	private boolean isSubmitSuccessNeedAssert;
	private boolean isMeasured = false;
	private int[] location = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.submit_success);
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		String orderCode = bundle.getString("OrderCode");
		
		SimpleDateFormat formatter =new SimpleDateFormat (getResources().getString(R.string.TimeFormat));    
		String orderDeliveryCycle= bundle.getString("OrderDeliveryCycle");
		Calendar cal = Calendar.getInstance();
		if (orderDeliveryCycle == null || orderDeliveryCycle.isEmpty()){
			orderDeliveryCycle ="10";
		}
		
		cal.add(Calendar.DAY_OF_MONTH, Integer.valueOf(orderDeliveryCycle));

		//获取当前时间    
		String str = formatter.format(cal.getTime());  
		TextView deliver_date=  (TextView) findViewById(R.id.deliver_date);
		deliver_date.setText("（" + str + "）");
		
		TextView tvOrderId=  (TextView) findViewById(R.id.order_number);
		tvOrderId.setText(orderCode);
		
		shareButton=(RadioButton) findViewById(R.id.radio_button_share);
		Button selectOrderButton = (Button)findViewById(R.id.btnSelectOrder);  
        Button returnHomeButton = (Button)findViewById(R.id.btnReturnHome); 
        Button returnButton = (Button)findViewById(R.id.btnReturn);
        shareButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(SubmitSuccessActivity.this,ShareActivity.class);
				startActivity(intent);
				finish();
			}
		});
        returnButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				
				Intent intent = new Intent();    
				intent.setClass(SubmitSuccessActivity.this, HomeChangeActivity.class);   
				SubmitSuccessActivity.this.startActivity(intent);
				finish();
			}
			
		});
        
        returnHomeButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				
				Intent intent = new Intent();    
				intent.setClass(SubmitSuccessActivity.this, HomeChangeActivity.class);   
				SubmitSuccessActivity.this.startActivity(intent);
				finish();
			}
		});
        
        selectOrderButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				
				Intent intent = new Intent();    
				intent.setClass(SubmitSuccessActivity.this, HomeChangeActivity.class); 
				intent.putExtra("FragmentIndex", 1);
				SubmitSuccessActivity.this.startActivity(intent);
			}
			
		});
        
        SharedPreferences preferences = this.getSharedPreferences(
				"assert_pref", Context.MODE_APPEND);
        isSubmitSuccessNeedAssert = preferences.getBoolean("isSubmitSuccessNeedAssert", true);
		if(isSubmitSuccessNeedAssert){
			ViewTreeObserver observer = shareButton.getViewTreeObserver();
			observer.addOnPreDrawListener(new OnPreDrawListener() {
				
				@Override
				public boolean onPreDraw() {
					if(isMeasured==false){
						location = new int[2];
						shareButton.getLocationInWindow(location);
						int width = shareButton.getMeasuredWidth();
						int height = shareButton.getMeasuredHeight();
						assertFragment=new AssertFragment(4,location[0]+width/2,location[1]+height/2,width);
						getFragmentManager().beginTransaction().add(R.id.layout_container, assertFragment).commit();
						isMeasured = true;
						isSubmitSuccessNeedAssert=false;
					}
					
					return true;
				}
			});
			
		}
        
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if(isSubmitSuccessNeedAssert){
				getFragmentManager().beginTransaction().remove(assertFragment).commit();
				SharedPreferences preferences = this.getSharedPreferences(
						"assert_pref", Context.MODE_APPEND);
				Editor editor = preferences.edit();
				editor.putBoolean("isSubmitSuccessNeedAssert", false);
				editor.commit();
				isSubmitSuccessNeedAssert=false;
			}else {
				Intent intent = new Intent();    
				intent.setClass(SubmitSuccessActivity.this, HomeChangeActivity.class);   
				SubmitSuccessActivity.this.startActivity(intent);
				finish();
			}
		}
		return false;
	}
}
